/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package uk.ac.cam.ccn.sac.modules.audio.message;

import uk.ac.cam.ccn.sac.modules.audio.message.AudioMessage;
import java.io.IOException;
import java.sql.Timestamp;
import org.ccnx.ccn.CCNHandle;
import org.ccnx.ccn.config.ConfigurationException;
import org.ccnx.ccn.impl.CCNFlowControl.SaveType;
import org.ccnx.ccn.io.content.ContentEncodingException;
import org.ccnx.ccn.protocol.ContentName;
import org.ccnx.ccn.protocol.MalformedContentNameStringException;
import uk.ac.cam.ccn.sac.net.CCNNet;

/**
 *
 * @author Thomas
 */
public class AudioListener extends Thread{
    private boolean isRunning=true;
    private CCNNet net;
    
    private CCNSerializableAudioMessageObject readAudio;
    private CCNSerializableAudioMessageObject writeAudio;
    private ContentName audioNameSpace;
    
    public AudioListener(CCNNet net, ContentName nameSpace) throws ConfigurationException, IOException, MalformedContentNameStringException{
        super("AudioListener");
        this.net = net;
        // we do the same for the audio
        // handle for reading text
        CCNHandle tempAudioReadHandle = CCNHandle.getHandle();
        // handle for wirtting text
        CCNHandle tempAudioWriteHandle = CCNHandle.open();
        audioNameSpace = ContentName.fromURI(nameSpace.toURIString()+"/audio/");
        readAudio= new CCNSerializableAudioMessageObject(audioNameSpace, (AudioMessage)null, SaveType.RAW, tempAudioReadHandle);
        readAudio.updateInBackground(true);
        
        // TODO should send something like ding-dong
        writeAudio = new CCNSerializableAudioMessageObject(audioNameSpace, (AudioMessage)null, SaveType.RAW, tempAudioWriteHandle);
        writeAudio.save();
    }
    
    @Override
    public void run(){
        Timestamp lastUpdate = null;
        do{
            try{
                synchronized(readAudio) {
                        readAudio.wait(500);
                }
            }catch(InterruptedException e){}
            try{
                if(readAudio.isSaved()){
                    Timestamp thisUpdate = readAudio.getVersion();
                    if(lastUpdate == null || thisUpdate.after(lastUpdate)){
                        lastUpdate=thisUpdate;
                        System.out.println("Got an audio update "+thisUpdate);
                        AudioMessage audio = readAudio.audioMessage();
                        playAudio(audio);
                    }                    
                }
            }catch(Exception e){
                System.out.println("Exception while reading audio from network");
            }
        }while(isRunning);
    }
    
    private void playAudio(AudioMessage audio){
        net.playAudio(audio);      
    }
    
    public void sendAudio(AudioMessage audio) throws ContentEncodingException, IOException{
        writeAudio.save(audio);
    }
    
    public void shutdown(){        
        if(readAudio!=null)
            readAudio.cancelInterest();
        isRunning = false;
    }
}
